package br.com.massai.model.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "PRODUCTION_GROUP")
@NamedQueries({
	@NamedQuery(name = "ProductionGroup.find", query = "SELECT p FROM ProductionGroup p"),
	@NamedQuery(name = "ProductionGroup.count", query = "SELECT COUNT(p) FROM ProductionGroup p"),
	@NamedQuery(name = "ProductionGroup.duplicate", query = "SELECT COUNT(p) FROM ProductionGroup p WHERE LOWER(p.nameGroup) = LOWER(:nameGroup) AND p.status = true")
})
public class ProductionGroup extends GenericEntity {

	private static final long serialVersionUID = -3540113624038907120L;

	@Id
	@SequenceGenerator(name = "seqProductionGroup", sequenceName = "seq_production_group", allocationSize = 1)
	@GeneratedValue(generator = "seqProductionGroup", strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(name = "NAME_GROUP", length = 255, nullable = false, columnDefinition = "VARCHAR(255)")
	private String nameGroup;

	@Column(name = "STATUS", columnDefinition = "BOOLEAN DEFAULT true")
	private boolean status;

	@Transient
	private List<GroupItem> groupItems;

	@Transient
	private List<Department> departments;

	public ProductionGroup() {
		this.status = true;
		groupItems = new ArrayList<GroupItem>();
		departments = new ArrayList<Department>();
	}

	@Override
	public Map<String, Object> getParameters() {
		super.clearParameters();

		if(this.nameGroup != null && !this.nameGroup.isEmpty()){
			super.addClauseWhere("LOWER(p.nameGroup) LIKE LOWER(:nameGroup)", ConditionType.AND);
			super.addParameters("nameGroup", "%" + this.nameGroup + "%");
		}

		super.addClauseWhere("p.status =:status", ConditionType.AND);
		super.addParameters("status", true);

		return super.getParameters();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof ProductionGroup)
			if(((ProductionGroup)obj).getId().equals(this.id)) return true;

		return false;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNameGroup() {
		return nameGroup;
	}

	public void setNameGroup(String nameGroup) {
		this.nameGroup = nameGroup;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public List<GroupItem> getGroupItems() {
		return groupItems;
	}

	public void setGroupItems(List<GroupItem> groupItems) {
		this.groupItems = groupItems;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
}
